package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.NonExistentPackageException;
import com.sky.exception.SetmealExistException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SeatmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class SeatmealServiceImpl implements SeatmealService {
    @Autowired
    private SetmealMapper seatmealMapper;

    @Override
    @ApiOperation(value = "分页查询套餐")
    public  PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = seatmealMapper.pageQuery(setmealPageQueryDTO);

        // 检查结果列表是否为空
        List<SetmealVO> result = page.getResult();
        if (result == null || result.isEmpty()) {
            // 如果结果为空，则返回空的分页结果
            return  new PageResult(0, Collections.emptyList());
        }

        long total = page.getTotal();
        return new PageResult(total, result);
    }

    @Override
    @ApiOperation(value = "新增套餐")
    public void save(SetmealDTO setmealDTO) {
        //先向数据库根据name查询套餐表，判断是否为空
        Setmeal setmeal =seatmealMapper.getByName(setmealDTO.getName());
        //如果不为空，则抛出套餐已存在异常
        if (setmeal != null) {
            throw new SetmealExistException(MessageConstant.SETMEAL_EXIST);
        }
        //如果为空，再拷贝属性，向数据库插入数据
        Setmeal newSetmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, newSetmeal);
        //设置修改时间、修改人、创建时间、创建人
        newSetmeal.setUpdateUser(BaseContext.getCurrentId());
        newSetmeal.setCreateUser(BaseContext.getCurrentId());
        newSetmeal.setCreateTime(LocalDateTime.now());
        newSetmeal.setUpdateTime(LocalDateTime.now());
        //插入套餐表
        seatmealMapper.insert(newSetmeal);
        //插入套餐菜品关系表, 遍历套餐菜品关系表，设置套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(newSetmeal.getId());
        });
        seatmealMapper.insertBatch(setmealDishes);



    }

    @Override
    public void startOrStop(Integer status, Long id) {
        // 根据id查询套餐信息
        Setmeal setmeal = seatmealMapper.getById(id);
        if (setmeal == null){
            throw  new NonExistentPackageException(MessageConstant.SETMEAL_NOT_FOUND);
        }
        // 设置修改状态
        setmeal.setStatus(status);
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        // 修改套餐状态
        seatmealMapper.update(setmeal);

    }

    @Override
    public void deleteById(List<Long> ids) {
        //先查询套餐表，判断是否为空
        for (Long id : ids) {
            Setmeal setmeal = seatmealMapper.getById(id);
            if (setmeal == null){
                //如果为空，则抛出套餐不存在异常
                throw  new NonExistentPackageException(MessageConstant.SETMEAL_NOT_FOUND);
            }
            if (setmeal.getStatus() == 1){
                //如果存在，再判断套餐是否处于启售状态，如果处于启售状态，则抛出套餐启售异常
                throw  new NonExistentPackageException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //如果处于停售状态，再看看有没有菜品关联，如果有，则抛出菜品关联异常
        for (Long id : ids) {
            List<SetmealDish> setmealDishes = seatmealMapper.getSetmealDishesBySetmealId(id);
            if (setmealDishes != null && !setmealDishes.isEmpty()){
                throw new RuntimeException("该套餐关联了菜品，无法删除");
            }
            seatmealMapper.deleteById(id);
            seatmealMapper.deleteSetmealDishesBySetmealId(id);
        }






    }

    @Override
    @ApiOperation(value = "根据id查询套餐")
    public SetmealVO getById(Long id) {
        Setmeal setmeal = seatmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        return setmealVO;
    }

    @Override
    @ApiOperation(value = "修改套餐")
    public void update(SetmealDTO setmealDTO) {
        //先根据id查询套餐
        Setmeal setmeal = seatmealMapper.getById(setmealDTO.getId());
        // 判断套餐是否存在
        if (setmeal == null){
            // 如果套餐不存在，则抛出套餐不存在异常
            throw new NonExistentPackageException(MessageConstant.SETMEAL_NOT_FOUND);
        }
        // 如果套餐存在，再判断套餐是否处于启售状态，如果处于启售状态，则抛出套餐启售异常，不能修改
        if (setmeal.getStatus() == 1){
            // 如果套餐处于启售状态，则抛出套餐启售异常，不能修改
            throw new NonExistentPackageException(MessageConstant.SETMEAL_ON_SALE);
        }
        // 修改套餐信息
        Setmeal setmeal2 = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal2);
        setmeal2.setUpdateUser(BaseContext.getCurrentId());
        setmeal2.setUpdateTime(LocalDateTime.now());
        seatmealMapper.update(setmeal2);
        //修改完套餐信息后，再删除套餐菜品关系表，再插入新的套餐菜品关系表
        seatmealMapper.deleteSetmealDishesBySetmealId(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        seatmealMapper.insertBatch(setmealDishes);

    }

}
